package com.wandian.news.mvvm.toutiao.toutiaofragment;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.bean.TouTiaoClassBean;
import com.wandian.news.bean.TouTiaoContentBean;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.AppSPKeyConfig.PROVINCE_NAME;
import static com.mylib.libcore.config.NetConfig.BASE_TOU_TIAO;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description 头条
 **/
public class TouTiaoFragmentViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApi;
    public MutableLiveData<TouTiaoContentBean> touTiaoContentBean =  new MutableLiveData<>();
    public TouTiaoFragmentViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }

    public boolean isReFlash =true;
    public void getContentData(String value, int page, boolean isReFlash){
        if(UIUtils.isEmpty(value)){
            touTiaoContentBean.setValue(null);
            return;
        }

        this.isReFlash = isReFlash;
        JsonObject jsonObject = new JsonObject();
        String url =BASE_TOU_TIAO+value+"/index";
        HashMap<String, String> hashMap = new HashMap<>();
        if(value.equals("areanews")){
            String city = AppContext.getInstance().getAppPreferencesHelper().getKeyValue(PROVINCE_NAME);
            if(!UIUtils.isEmpty(city)){
                String province = city.replace("省","");
                hashMap.put("areaname", province.replace("市",""));
            }
        }
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("num","20");
        hashMap.put("page",page+"");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoContent(url,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            Log.d("getTouTiaoContent", JsonUtil.toJson(o)+"");
                            TouTiaoContentBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),TouTiaoContentBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200){
                                touTiaoContentBean.setValue(bean);
                            }else {
                                ToastUtil.show("数据异常，请联系客服");
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.show("网络错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
